package com.sihan.swvp.action.zc;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sihan.swvp.action.BaseAction;
import com.sihan.swvp.entity.zc.Zcgl;
import com.sihan.swvp.service.jc.JcBdzService;
import com.sihan.swvp.service.jc.JcBzService;
import com.sihan.swvp.service.rw.ZcglService;
import com.sihan.swvp.service.sys.LogService;

@Controller
@RequestMapping("/zcglAction")
public class ZcglAction extends BaseAction{

	@Autowired
	private JcBdzService jcBdzService;
	@Autowired
	private ZcglService zcglService;
	@Autowired
	public LogService logService;
	
	
	@ResponseBody
	@RequestMapping("/getZcglTree") 
	public List<Map<String, Object>> getZcglTree(){
		Map<String, Object> params = new HashMap<String, Object>();
			params.put("leaf", 1);
			List<Map<String, Object>> cjzlist=zcglService.getZcgl(params);//得到采集站
			
			List<Map<String, Object>> twolist= new ArrayList<Map<String,Object>>();
			for (int x = 0; x < cjzlist.size(); x++) {
				 Map<String, Object> twoMap=new HashMap<String, Object>();
				 twoMap.put("name", cjzlist.get(x).get("name"));
				 twoMap.put("id", cjzlist.get(x).get("id"));
				 twoMap.put("code",cjzlist.get(x).get("code"));
				 twoMap.put("ms",cjzlist.get(x).get("ms"));
				 twoMap.put("pid", cjzlist.get(x).get("id"));
				 twoMap.put("leaf",2);
				 twoMap.put("iconCls", "icon-open");
				 
				 params.put("pid", cjzlist.get(x).get("id"));
				 params.put("leaf", 2);
				 List<Map<String, Object>> jsData=zcglService.getZcgl(params);//得到设备终端
				 List<Map<String, Object>> onelist=new ArrayList<Map<String,Object>>();
				 for (int i = 0; i < jsData.size(); i++) {
					 Map<String, Object> mapjs=new HashMap();
					 mapjs.put("name", jsData.get(i).get("name"));
					 mapjs.put("pid", cjzlist.get(x).get("id"));//上级菜单id
					 mapjs.put("leaf",true);
					 mapjs.put("isLeaf", "str");
					 mapjs.put("code",jsData.get(i).get("code"));
					 mapjs.put("ms",jsData.get(i).get("ms"));
					 mapjs.put("sbid", jsData.get(i).get("id"));//设备Id
					 onelist.add(mapjs);
				 }
				 twoMap.put("children", onelist);
				 twolist.add(twoMap);
			}
		return twolist;
	}
	
	@ResponseBody
	@RequestMapping("/getZcglTree2") 
	public List<Map<String, Object>> getZcglTree2(@Param("node")String node,@Param("leaf")String leaf){
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("pid", node);
		params.put("leaf", leaf);
		List<Map<String, Object>> twolist= new ArrayList<Map<String,Object>>();
		if(leaf.equals("1")){//刷新一级
			List<Map<String, Object>> cjzlist=zcglService.getZcgl(params);//得到采集站
			for (int x = 0; x < cjzlist.size(); x++) {
				Map<String, Object> twoMap=new HashMap<String, Object>();
				twoMap.put("name", cjzlist.get(x).get("name"));
				twoMap.put("id", cjzlist.get(x).get("id"));
				twoMap.put("leaf",2);
				twoMap.put("code",cjzlist.get(x).get("code"));
				twoMap.put("ms",cjzlist.get(x).get("ms"));
				twoMap.put("pid", node);
				twoMap.put("iconCls", "icon-open");
				
				params.put("pid", cjzlist.get(x).get("id"));
				params.put("leaf", 2);
				List<Map<String, Object>> jsData=zcglService.getZcgl(params);//得到设备终端
				List<Map<String, Object>> onelist=new ArrayList<Map<String,Object>>();
				for (int i = 0; i < jsData.size(); i++) {
					Map<String, Object> mapjs=new HashMap();
					mapjs.put("name", jsData.get(i).get("name"));
					mapjs.put("pid", cjzlist.get(x).get("id"));//上级菜单id
					mapjs.put("leaf",true);
					mapjs.put("isLeaf", "str");
					mapjs.put("code",jsData.get(i).get("code"));
					mapjs.put("ms",jsData.get(i).get("ms"));
					mapjs.put("sbid", jsData.get(i).get("id"));//设备Id
					onelist.add(mapjs);
				}
				twoMap.put("children", onelist);
				twolist.add(twoMap);
			}
		}else{//刷新二级
			List<Map<String, Object>> jsData=zcglService.getZcgl(params);//得到设备终端
			for (int i = 0; i < jsData.size(); i++) {
				Map<String, Object> mapjs=new HashMap();
				mapjs.put("name", jsData.get(i).get("name"));
				mapjs.put("pid", node);//上级菜单id
				mapjs.put("leaf",true);
				mapjs.put("isLeaf", "str");
				mapjs.put("code",jsData.get(i).get("code"));
				mapjs.put("ms",jsData.get(i).get("ms"));
				mapjs.put("sbid", jsData.get(i).get("id"));//设备Id
				twolist.add(mapjs);
			}
		}
		
		
		return twolist;
	}
	
	
	@ResponseBody
	@RequestMapping("/addZcgl")
	public Object addZcgl(Zcgl bean,@Param("isLeaf")String isLeaf){
		try {
			bean.setUpuser(getLoginUser().getId());
			Map<String, Object> param=new HashMap<String, Object>();
			int left=2;
			param.put("pid", bean.getPid());
			if(bean.getLeaf()==null){
				bean.setLeaf(1);
				left=1;
			}
			param.put("leaf", left);
			param.put("code", bean.getCode());
			Integer codeNumber=zcglService.getZcglValidate(param);
			param.put("notCode", "str");
			param.put("name", bean.getName());
			Integer nameNumber=zcglService.getZcglValidate(param);
			if(codeNumber !=0){
				return failMsg("设备编码已存在");
			}else if(nameNumber !=0){
				return failMsg("设备名称已存在");
			}
			zcglService.addZcgl(bean);
			logService.addUserLog("添加设备", "设备", getLoginUser().getId(), "添加设备："+bean.getName(),getRemoteAddr(),1,0);
			return successMsg("");
		} catch (Exception e) {
			e.printStackTrace();
			logService.addUserLog("添加设备", "设备", getLoginUser().getId(), "添加设备："+bean.getName(),getRemoteAddr(),0,0);
			return failMsg();
		}
	}
	
	
	@ResponseBody
	@RequestMapping("/updateZcgl")
	public Object updateZcgl(Zcgl bean){
		try {
			bean.setUpuser(getLoginUser().getId());
			Map<String, Object> param=new HashMap<String, Object>();
			param.put("id", bean.getId());
			int left=1;
			if(bean.getLeaf()==null){
				param.put("pid", bean.getPid());
				left=2;
			}
			param.put("leaf", left);
			param.put("code", bean.getCode());
			Integer codeNumber=zcglService.getZcglValidate(param);
			param.put("notCode", "str");
			param.put("name", bean.getName());
			Integer nameNumber=zcglService.getZcglValidate(param);
			if(codeNumber !=0){
				return failMsg("设备编码已存在");
			}else if(nameNumber !=0){
				return failMsg("设备名称已存在");
			}
			zcglService.updateZcgl(bean);
			logService.addUserLog("修改设备", "设备", getLoginUser().getId(), "修改设备："+bean.getName(),getRemoteAddr(),1,0);
			return successMsg("");
		} catch (Exception e) {
			e.printStackTrace();
			logService.addUserLog("修改设备", "设备", getLoginUser().getId(), "修改设备："+bean.getName(),getRemoteAddr(),0,0);
			return failMsg();
		}
	}
	
	@ResponseBody
	@RequestMapping("/deleteZcgl")
	public Object deleteJcBz(@Param("id")Integer id,@Param("leaf")String leaf,@Param("name")String name){
		try {
			if(!leaf.equals("2")){
				leaf="";
			}
			zcglService.deleteZcgl(id,leaf);
			logService.addUserLog("删除设备", "设备", getLoginUser().getId(), "删除设备："+name,getRemoteAddr(),1,0);
			return successMsg("");
		} catch (Exception e) {
			e.printStackTrace();
			logService.addUserLog("删除设备", "设备", getLoginUser().getId(), "删除设备："+name,getRemoteAddr(),0,0);
			return failMsg();
		}
	}
}
